package com.jwx.mycar.controller;


import com.jwx.mycar.mapper.UserMapper;
import com.jwx.mycar.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @program: mycar
 * @description:
 * @author: jwx
 * @create: 2019-11-15 17:51
 **/
@RestController
public class UserController2 {
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;

    /**
     * 登录入口
     *
     * @param username 用户名
     * @param password 密码
     * @param code     验证码
     */
    @ApiOperation("登录")
    @PostMapping("/login3")
    public void adminLoginByPasswword(@ApiParam("用户名") @RequestParam String username, @ApiParam("密码") @RequestParam String password, @ApiParam("验证码") @RequestParam String code, HttpServletResponse response, HttpServletRequest request) throws IOException {
        HttpSession session = request.getSession();
        PrintWriter out = response.getWriter();
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        if (session.getAttribute("imageCode") == null) {
            out.print("<script language=\"javascript\">alert('请刷新验证码');</script>");

        } else {
            if (session.getAttribute("imageCode").toString().equalsIgnoreCase(code)) {
                {
                    String password1 = userMapper.selectPasswordByUsername(username);
                    if (password1.equals(password)) {
                        session.setAttribute("users", username);//用户名存入该用户的session 中
                        out.print("<script language=\"javascript\">alert('登录成功了');window.location.href='/notifications'</script>");
                    } else
                        out.print("<script language=\"javascript\">alert('账号密码错误');window.location.href='/loging'</script>");
                }

            }
            else {

                out.print("<script language=\"javascript\">alert('验证码错误');</script>");
            }

        }
    }
//    @ApiOperation("生成验证码")
//    @GetMapping("/..getCode")
//    public void getCode(HttpServletResponse response, HttpServletRequest request) throws Exception{
//        HttpSession session=request.getSession();
//        //利用图片工具生成图片
//        //第一个参数是生成的验证码，第二个参数是生成的图片
//        Object[] objs = VerifyUtil.createImage();
//        //将验证码存入Session
//        session.setAttribute("imageCode",objs[0]);
//
//        //将图片输出给浏览器
//        BufferedImage image = (BufferedImage) objs[1];
//        response.setContentType("image/png");
//        OutputStream os = response.getOutputStream();
//        ImageIO.write(image, "png", os);
//    }

}
